﻿using FAMailUtils;
using FAMailUtils.Data.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineBusinesses.Areas.CustomerAdmin
{
    public partial class AccountList : System.Web.UI.Page
    {
        AccountDAO dao = new AccountDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                lvAccount.DataSource = dao.GetAll(((AdminCustomer_Master)Page.Master).getUserLogin().Client.ClientID);
                lvAccount.DataBind();
            }
            catch (Exception ex)
            {
                Common.logs(Request.RawUrl + "-" + Common.GetCurrentMethod(), ex, Common.logType.error);
            }
        }

        protected void btnDel_Click(object sender, CommandEventArgs e)
        {
            try
            {
                dao.Delete(Convert.ToInt32(e.CommandArgument), ((AdminCustomer_Master)Page.Master).getUserLogin().Client.ClientID);
                Response.Redirect(Request.RawUrl);
            }
            catch (Exception ex)
            {
                Common.logs(Request.RawUrl + "-" + Common.GetCurrentMethod(), ex, Common.logType.error);
            }
            
        }

        protected void btnBlock_Click(object sender, CommandEventArgs e)
        {
            try
            {
                dao.Block(Convert.ToInt32(e.CommandArgument), ((AdminCustomer_Master)Page.Master).getUserLogin().Client.ClientID);
                Response.Redirect(Request.RawUrl);
            }
            catch (Exception ex)
            {
                Common.logs(Request.RawUrl + "-" + Common.GetCurrentMethod(), ex, Common.logType.error);
            }
            
            
        }

        public static string GetMd5Hash(string input)
        {
            MD5 md5Hash = MD5.Create();
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }

        protected void btnResetPassword_Click(object sender, CommandEventArgs e)
        {
            try
            {
                dao.ResetPassword(Convert.ToInt32(e.CommandArgument), ((AdminCustomer_Master)Page.Master).getUserLogin().Client.ClientID, GetMd5Hash("12345"));
                lbNotice.Text = "Mật khẩu đã được làm mới.";
            }
            catch (Exception ex)
            {
                Common.logs(Request.RawUrl + "-" + Common.GetCurrentMethod(), ex, Common.logType.error);
            }


        }
    }
}